package ModelLayer;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * Write a description of class LoanContainer here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class LoanContainer
{
    // instance variables - replace the example below with your own
    private ArrayList<Loan> loanlist;    
    private static LoanContainer instance; //Singleton
    
    

    /**
     * Singleton constructor
     */
    private LoanContainer()
    {      
        loanlist = new ArrayList<Loan>();
    }
    
    //Singleton
    public static LoanContainer getInstance()
    {
        if(instance == null)
        {
            instance = new LoanContainer();
        }
        return instance;
    }

    //add Loan to ArrayList<Person>
    public void addLoan(Loan d)
    {
        loanlist.add(d);
    }

    //find person in ArrayList<Person>
    public Loan findLoan(int no)
    {
        Loan loan = null;
        int index = 0;
        int rememberIndex = 0;
        boolean found = false;

        while(index < loanlist.size() && !found)
        {
            loan = loanlist.get(index);

            if(loan.getID() == no)
            {
                found = true;
                rememberIndex = index;
            }
            index++;
        }
        if(found)
        {
            return loanlist.get(rememberIndex);
        }
        else
        {
            return null;
        }
    }

    //update loan in ArrayList<Loan>
    public void updateLoan(int no, DVD dvd, Copy copy, Person person, String loandate, String returndate, boolean active)
    {
        Loan loan = null;
        int index =  0;
        int rememberIndex = 0;
        boolean found = false;

        while(index < loanlist.size() && !found)
        {
            loan = loanlist.get(index);

            if(loan.getID() == no)
            {
                found = true;
                rememberIndex = index;
            }
            index++;
        }
        if(found)
        {
            loan = loanlist.get(rememberIndex);
            loan.setDVD(dvd);
            loan.setCopy(copy);
            loan.setPerson(person);
            loan.setLoandate(loandate);
            loan.setReturndate(returndate);
            loan.setActive(active);
            System.out.println("the Loan: "+no+" has been updated");
        }
        else
        {
            System.out.println("The Loan: "+no+" dosen't exist");
        }

    }
    
    public void deleteLoan(int no)
    {
        int index = 0;
        int rememberIndex = 0;
        boolean found = false;
        Loan loan = null;
        
        while(index < loanlist.size() && !found)
        {
            loan = loanlist.get(index);
            
            if(loan.getID() == no)
            {
                found = true;
                rememberIndex = index;
            }
            index++;
            
        }
        if(found)
        {
            System.out.println("The Loan with id "+no+" has been removed.");
            loanlist.remove(rememberIndex);
        }
        else
        {
            System.out.println("The Loan with id "+no+" dosen't exist");
        }
        
    }
    
    public void printDetailAll()
    {
        if(loanlist != null)
        {
            for(Loan filename : loanlist)
            {
                filename.printDetail();
            }
        }
        else
        {
            System.out.println("There is no loans.");
        }
    }
    
    public int getSizeOfLoan()
    {
        return loanlist.size();
    }
}
